package com.mokee.android.Pay;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.mokee.Util.ShipAttrUtil;
import com.mokee.Util.ToastUtil;
import com.mokee.android.SharedPreference.MyValuesInt;
import com.mokee.android.SharedPreference.MyValuesString;
import com.mokee.android.ShipPass.R;
import com.mokee.android.database.DataBaseHelper;
import com.mokee.android.database.UserTable;

public class PayNewActivity extends Activity implements OnClickListener, OnItemSelectedListener {
	private static final String tag = "PayNewActivity";
	
	private EditText et_RegisterNum, et_ShipWeightOri, et_ShipType, et_ShipName, et_GoodsName, 
					 et_SailEnd, et_SailStart, et_WeightNow;
	private Spinner sp_LoadType, sp_SafeLevel, sp_Sail_Orientation;
	private int loadType = 0, safeLevel = 0, sailOrienation = 0;
	private TextView layout_sub_Title, iv_Submit;
	private LinearLayout layout_sub_Add;
	private Button btn_TopReturn;
	
	private int shipNowWeight = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_pay_new);
		
		initView();
	}

	private void initView() {
		et_RegisterNum = (EditText) findViewById(R.id.et_RegisterNum);
		et_ShipWeightOri = (EditText) findViewById(R.id.et_ShipWeightOri);
		et_ShipType = (EditText) findViewById(R.id.et_ShipType);
		et_ShipName = (EditText) findViewById(R.id.et_ShipName);
		
		et_GoodsName = (EditText) findViewById(R.id.et_GoodsName);
		et_SailEnd = (EditText) findViewById(R.id.et_SailEnd);
		et_SailStart = (EditText) findViewById(R.id.et_SailStart);
		et_WeightNow = (EditText) findViewById(R.id.et_WeightNow);
		
		btn_TopReturn = (Button) findViewById(R.id.btn_TopReturn);
		btn_TopReturn.setOnClickListener(this);
		
		sp_Sail_Orientation = (Spinner) findViewById(R.id.sp_Sail_Orientation);
		sp_SafeLevel = (Spinner) findViewById(R.id.sp_SafeLevel);
		sp_LoadType = (Spinner) findViewById(R.id.sp_LoadType);
		
		sp_LoadType.setOnItemSelectedListener(this);
		sp_SafeLevel.setOnItemSelectedListener(this);
		sp_Sail_Orientation.setOnItemSelectedListener(this);
		
		layout_sub_Title = (TextView) findViewById(R.id.layout_sub_Title);
		layout_sub_Add = (LinearLayout) findViewById(R.id.layout_sub_Add);
		iv_Submit = (TextView) findViewById(R.id.iv_Submit);
		
		layout_sub_Title.setText(R.string.process_new_tip);
		iv_Submit.setText(R.string.save_tip);
		
		layout_sub_Add.setOnClickListener(this);
		
		et_ShipName.setText(MyValuesString.getShipName());
		et_ShipType.setText(ShipAttrUtil.shipTypeInt2String(MyValuesInt.getShipType()));
		et_ShipWeightOri.setText(String.valueOf(MyValuesInt.getShipWeightOri()));
		et_RegisterNum.setText(MyValuesString.getRegisterNum());
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_sub_Add:
			if(checkInput()){
				
				String sailStart = et_SailStart.getText().toString().trim();
				String sailEnd = et_SailEnd.getText().toString().trim();
				String shipGoodsName = et_GoodsName.getText().toString().trim();				
				shipNowWeight = Integer.valueOf(et_WeightNow.getText().toString().trim());
				
				UserTable user = new UserTable();
				
				user.setUserLoginNumber(MyValuesString.getLoginName()).setUserNickName(MyValuesString.getNikeName())
				.setUserPhone(MyValuesString.getPhone()).setUserIDCard(MyValuesString.getIDCard())
				.setShipWeightOri(MyValuesInt.getShipWeightOri()).setShipWeightNow(shipNowWeight)
				.setShipType(MyValuesInt.getShipType()).setShipSailStart(sailStart).setShipSailEnd(sailEnd)
				.setShipSailOrientation(sailOrienation).setShipSafeLevel(safeLevel).setShipRegisterNum(MyValuesString.getRegisterNum())
				.setShipProcess(0).setShipName(MyValuesString.getShipName()).setShipGoodsName(shipGoodsName)
				.setShipLoadType(loadType);
				
				Log.i(tag, "PayNewActivity.onClick.layout_sub_Add:" + user.toString());
				int returnInt = DataBaseHelper.getInstance().getUserRecord().insert(user);
				Log.i(tag, "returnInt:" + returnInt);
				ToastUtil.Toast_LS(getApplicationContext(), R.string.toast_new_pay_success);
				
				finish();
				onDestroy();
			} else {
				ToastUtil.Toast_LS(this, R.string.toast_register_submit_fail);
			}
			break;
		case R.id.btn_TopReturn:
			finish();
			onDestroy();
			break;

		default:
			break;
		}
	}

	private boolean checkInput() {
		boolean result = false;
		if(et_GoodsName.getText().toString().trim().equals("")){
			return result;
		}
		if(et_SailEnd.getText().toString().trim().equals("")){
			return result;
		}
		if(et_SailStart.getText().toString().trim().equals("")){
			return result;
		}
		if(et_SailEnd.getText().toString().trim().equals("")){
			return result;
		}
		if(et_WeightNow.getText().toString().trim().equals("")){
			return result;
		}
		result = true;
		return result;
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		switch (view.getId()) {
		case R.id.sp_LoadType:
			loadType = position;
			break;
		case R.id.sp_SafeLevel:
			safeLevel = position;
			break;
		case R.id.sp_Sail_Orientation:
			sailOrienation = position;
			break;

		default:
			break;
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
	}
}
